<!--
	作者：offline
	时间：2018-09-03
	描述：我的对账列表
-->
<template>
	<div class="m-view-myAccountList m-window">
		<div class="m-condition" v-if="diaShow">
			<el-row :gutter="10">
				<el-col :span="6">
					<el-select
						value-key="id"
						v-model="selCompanyCode"
						:placeholder="placeholder"
						@change="companyChange"
						clearable
					>
						<el-option v-for="item in comOptions" :key="item.id" :label="item.companyName" :value="item"></el-option>
					</el-select>
				</el-col>
				<el-col :span="6">
					<el-select v-model="selCusCode" placeholder="请选择供应商" clearable v-if="optionsShow">
						<el-option
							v-for="item in cusOptions"
							:key="item.id"
							:label="item.supplierName"
							:value="item.id"
						></el-option>
					</el-select>
				</el-col>
				<el-col :span="6">
					<el-input v-model="accountNo" placeholder="请输入单号" clearable></el-input>
				</el-col>
			</el-row>
			<el-row :gutter="10">
				<el-col :span="6">
					<el-date-picker
						v-model="applyDate"
						type="daterange"
						range-separator="至"
						start-placeholder="申请日期"
						end-placeholder="申请日期"
						value-format="yyyy-MM-dd"
					></el-date-picker>
				</el-col>
				<el-col :span="3">
					<el-select v-model="recCode" placeholder="单据状态" clearable>
						<el-option
							v-for="item in recOptions"
							:key="item.value"
							:label="item.label"
							:value="item.value"
						></el-option>
					</el-select>
				</el-col>
				<el-col :span="3">
					<el-select v-model="signCode" placeholder="签署状态" clearable v-if="selShow">
						<el-option
							v-for="item in signOptions"
							:key="item.value"
							:label="item.label"
							:value="item.value"
						></el-option>
					</el-select>
				</el-col>
				<el-col :span="2">
					<el-button type="primary" @click="search">查询</el-button>
				</el-col>
				<el-col :span="3">
					<el-button type="primary" @click="addPhyCheck" v-if="btnShow">新增实物对账</el-button>
				</el-col>
				<el-col :span="3">
					<el-button type="primary" @click="addAccountCheck" v-if="btnShow">新增会计对账</el-button>
				</el-col>
				<el-col :span="2">
					<el-button type="primary" @click="del" v-if="btnShow">删除</el-button>
				</el-col>
			</el-row>
		</div>
		<div class="m-agTable m-top2 m-bottom" v-if="diaShow">
			<el-tabs v-model="activeName" @tab-click="handleClick" type="card">
				<el-tab-pane label="实物对账" name="first">
					<physicalCheckInfo ref="getPhysicalInfo"></physicalCheckInfo>
				</el-tab-pane>
				<el-tab-pane label="会计对账" name="second">
					<accountCheckInfo ref="getAccountInfo"></accountCheckInfo>
				</el-tab-pane>
			</el-tabs>
		</div>

		<!--新增实物对账-->
		<physicalApplyComponents v-if="phyShow"></physicalApplyComponents>
		<!--  -->
	</div>
</template>

<script>
import _Api from "@/api";
// import localeText from "@/i18n/agGridText";
import physicalCheckInfo from "@/views/accountManagement/physicalCheckInfo.vue"
import accountCheckInfo from "@/views/accountManagement/accountCheckInfo.vue"
import orderInfo from "@/views/accountManagement/orderInfo.vue"
import storageInfo from "@/views/accountManagement/storageInfo.vue"
import physicalApplyComponents from "@/views/accountManagement/physicalApplyComponents.vue"
import supplierInfo from "@/public/supplierInfo.js";
import Msg from "@/public/message.js";

export default {
	data () {
		return {
			selCusCode: "",
			cusOptions: [],
			selSupplierCode: "",
			supOptions: [],
			selCompanyCode: "",
			comOptions: [],
			accountNo: "",
			applyDate: "",
			recCode: "",
			//单据状态
			recOptions: [
				{ value: 1, label: "新增" },
				{ value: 2, label: "提交" },
				{ value: 3, label: "已确认" },
				{ value: 4, label: "退回" }],
			signCode: "",
			//签署状态
			signOptions: [
				{ value: 1, label: "未进行" },
				{ value: 2, label: "进行中" },
				{ value: 3, label: "已完成" }],
			activeName: "first",
			selShow: false,
			userTypeId: "",
			placeholder: "请选择公司",
			optionsShow: false,
			btnShow: false,
			phyShow: false,
			diaShow: true,
			phyCode: "",
			supplierName: "",
			remark: "",
			physicalInfo: [],
			accountInfo: [],
			userInfo: {},
			supplierInfo: ""
		}
	},
	beforeMount () {

	},
	methods: {
		search () {
			//查询方法
			var obj = {};
			// var supplier = JSON.parse(localStorage.getItem('supplier'));
			if (this.selCompanyCode == "") {
				Msg.showPopMsg("提示：" + this.placeholder)
				return;
			}
			if (this.$global.user.userTypeId == "4")
				obj.supplierId = this.supplierInfo[0].id;
			else
				obj.supplierId = this.selCusCode;

			if (this.selCompanyCode)
				obj.companyId = this.selCompanyCode.id;
			if (this.accountNo)
				obj.odd = this.accountNo;
			if (this.applyDate) {
				obj.begin_date = this.applyDate[0];
				obj.end_date = this.applyDate[1];
			}
			if (this.recCode == "" || !this.recCode)
				obj.billState = 0;
			else
				obj.billState = this.recCode;
			if (this.signCode == "" || !this.signCode)
				obj.signState = 0;
			else
				obj.signState = this.signCode;
			var that = this;

			_Api.getAccountList(obj).then(function (response) {
				var data = response.data;
				if (data.code == "1") {
					if (Array.isArray(data.rows.myTblAccount)) {
						that.accountInfo = data.rows.myTblAccount;
						that.$refs.getAccountInfo.getDataRows(that.accountInfo);
					}
					if (Array.isArray(data.rows.myTblGoodsAccount)) {
						that.physicalInfo = data.rows.myTblGoodsAccount;
						that.$refs.getPhysicalInfo.getDataRows(that.physicalInfo);
					}
				} else {
					Msg.showErrorMsg(data.info);
				}
			}).catch((err) => {
				Msg.showErrorMsg(err.data.message);
			})
		},
		addPhyCheck () {
			//新增实物对账
			this.diaShow = false;
			this.phyShow = true;
			this.supplierName = this.$global.user.employeeName + ' (' + this.$global.user.userName + ')';
		},
		addAccountCheck () {
			//新增会计对账
		},
		del () {
			//删除
			if (this.activeName == "first") {
				this.$refs.getPhysicalInfo.getSelectedRow();
			} else if (this.activeName == "second") {
				this.$refs.getAccountInfo.getSelectedRow();
			}
		},
		handleClick (tab) {
			//tab页点击事件
			if (tab.index == 0)
				this.selShow = false;
			else if (tab.index == 1)
				this.selShow = true;
		},
		getAuthInfo () {
			//获取当前账号权限公司
			var obj = {};
			if (this.$global.user.userTypeId == 4) {
				obj.account = this.$global.user.userName;
				obj.userType = 2;
			}
			else if (this.$global.user.userTypeId == 1) {
				obj.account = this.$global.user.oaNum;
				obj.userType = 1;
			}
			var that = this;
			_Api.getAuthInfo(obj).then(function (response) {
				var data = response.data;
				if (data.code == "1") {
					that.comOptions = data.rows.myMstCompany;
					that.cusOptions = data.rows.myMstSupplier;
				} else {
					that.comOptions = [];
					that.cusOptions = [];
				}
			}).catch((err) => {
				Msg.showErrorMsg(err.data.message);
				that.comOptions = [];
				that.cusOptions = [];
			})
		},
		companyChange (value) {
			//公司下拉框选择
			if (value) {
				var obj = {
					name: value.companyCode
				};
				var that = this;
				_Api.getAuthSupplierByCompanyCode(obj).then(function (response) {
					var data = response.data;
					if (data.code == "1") {
						if (data.total > 0)
							that.cusOptions = data.rows;
						else
							that.cusOptions = [];
					}
				}).catch((err) => {
					Msg.showErrorMsg(err.data.message);
					that.cusOptions = [];
				})
			} else {
				this.getAuthInfo();
			}
		}
	},
	mounted () {
		this.getAuthInfo();
		this.userTypeId = this.$global.user.userTypeId;
		if (this.userTypeId) {
			if (this.userTypeId == 4) {
				var that = this;
				this.btnShow = true;
				this.optionsShow = false;
				this.placeholder = "请选择客户";
				supplierInfo.getUserInfo(this.$global.user.userName).then((supplierInfo) => {
					that.supplierInfo = supplierInfo;
				})
			}
			else
				this.optionsShow = true;
		}
	},
	components: {
		physicalCheckInfo,
		accountCheckInfo,
		orderInfo,
		storageInfo,
		physicalApplyComponents
	}

}
</script>